package org.belkodevelop.form.validator;

import com.google.code.kaptcha.Constants;
import javax.servlet.http.HttpServletRequest;
import org.belkodevelop.form.RegistrationForm;
import org.belkodevelop.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.apache.commons.lang.StringUtils;
import org.belkodevelop.user.domain.User;
import org.belkodevelop.user.exception.UserNotFoundException;

@Component
public class RegistrationValidator {
        
        @Autowired
        private UserService userService;

        public void validate(RegistrationForm reg, Errors errors, HttpServletRequest request) {
            reg.trim();
            validateFullName(errors);
            validateEmail(reg, errors);
            validatePasswords(reg, errors);
            if (!errors.getFieldErrors().isEmpty()) {
                return;
            }  
            validateCaptcha(reg, errors, request);
        }
        
        private void validateFullName(Errors errors) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "fullName", "validator_error.full_name_is_empty");
        }

        private void validateEmail(RegistrationForm reg, Errors errors) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "validator_error.email_is_empty");
            if (!errors.getFieldErrors("email").isEmpty()) {
                return;
            }                
            EmailValidator emailValidator = new EmailValidator();
            if (!emailValidator.validate(reg.getEmail())) {
                errors.rejectValue("email", "validator_error.invalid_email_address");
            }
            if (!errors.getFieldErrors("email").isEmpty()) {
                return;
            }                 
            try {
                User user = userService.getUserByLogin(reg.getEmail());
                if (user != null) {
                    errors.rejectValue("email", "validator_error.user_exists");
                }
            } catch (UserNotFoundException ex) {                
            }
        }

        private void validatePasswords(RegistrationForm reg, Errors errors) {
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "validator_error.empty_password");
            if (!errors.getFieldErrors("password").isEmpty()) {
                return;
            }
            if (reg.getPassword() != null && reg.getConfirmPassword() != null)
            {    
                if (!reg.getPassword().equals(reg.getConfirmPassword())) {
                    errors.rejectValue("confirmPassword", "validator_error.passwords_are_not_equal");
                }
            }   
        }  
        
        private void validateCaptcha(RegistrationForm reg, Errors errors, HttpServletRequest request) {
            String captchaCodeGen = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
            request.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);            
            ValidationUtils.rejectIfEmptyOrWhitespace(errors, "captchaCode", "validator_error.captcha_code_is_empty");
            if (!errors.getFieldErrors("captchaCode").isEmpty()) {
                return;
            }
            if (!StringUtils.equalsIgnoreCase(captchaCodeGen, reg.getCaptchaCode())) {
                errors.rejectValue("captchaCode", "validator_error.error_captcha");
            }
        }
}
